package com.example.lijun.myapplication;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.io.IOException;
import java.io.InputStream;

import tools.Utils;

public class MainActivity extends AppCompatActivity {
    private LettersView lettersView;
    private LettersText lettersText;

    private String TAG = "MainActivity";
    private static final String fileName = "font.json";

    public String fontResources;
    public Utils utils;
    private InputStream inputStream;

    //输入框内容
    private String editTextContext;

    //输入框中的字符
    private char input;
    private int enterTimes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFontResources();

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.letters);

        lettersView = (LettersView) findViewById(R.id.letter_view);

        lettersView.setFontResources(fontResources);

        lettersText = new LettersText(this);
        lettersText.setBackgroundColor(Color.YELLOW);

        linearLayout.addView(lettersText);


        lettersText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d(TAG, "bbb " + s.toString());
                //检测到回车键

                editTextContext = lettersText.getText().toString();
                lettersView.getLetterPath(editTextContext);

                //TODO OnDraw 更新问题
                if (s.length() > 0) {
                    if (s.charAt(s.length() - 1) != '\n') {
                        lettersView.invalidate();
                    }
                }

                Log.d(TAG, " xxxx " + editTextContext);

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    //强制打开软键盘
    public void showSoftInputFromWindow(Activity activity, LettersText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        //打开软键盘
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

    public void initFontResources() {
        utils = new Utils();
        try {
            inputStream = getAssets().open(fileName);
            fontResources = utils.ReadFile(inputStream);
        } catch (IOException e) {
            Log.d(TAG, "get font  resource error " + e.getMessage());
        }

        //TODO 初始化问题
        char letter = 'A';
        utils.getLetterPath(fontResources, letter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
